package com.wfh.activity;

import com.wfh.R;
import com.wfh.R.layout;
import com.wfh.R.menu;
import com.wfh.share.QQShare;
import com.wfh.share.SinaWeiBo;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LoginActivity extends CommonActivity {

	final String numberpattern = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
	final String passwordpattern = "^(\\S+[a-z A-Z 0-9]){2,}$";
	private EditText username, password;
	private TextView regist;
	private Button login;
	private TextView findpassword, title;
	private MyDialog dialog;
	private boolean mBound = true;
	private BroadcastReceiver datareceiver;
	private String user;
	private String pass;
	private Button loginout, bound;
	private RelativeLayout login1, login2;
	private ImageButton qq, sina;
	private SinaWeiBo sinaapi;//sinaapi
	private QQShare qqapi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		sinaapi=new SinaWeiBo(this);
		qqapi=new QQShare(this);
		super.onCreate(savedInstanceState, R.layout.activity_login);

		title = (TextView) findViewById(R.id.activty_title);
		username = (EditText) findViewById(R.id.editText1);
		password = (EditText) findViewById(R.id.editText2);
		login = (Button) findViewById(R.id.button1);
		dialog = MyDialog.create(this);
		findpassword = (TextView) findViewById(R.id.textView3);
		regist = (TextView) findViewById(R.id.textView4);
		qq = (ImageButton) findViewById(R.id.imageButton1);
		sina = (ImageButton) findViewById(R.id.imageButton2);

		// 登陆后
		loginout = (Button) findViewById(R.id.loginout);
		bound = (Button) findViewById(R.id.bound);
		login1 = (RelativeLayout) findViewById(R.id.login1);
		login2 = (RelativeLayout) findViewById(R.id.login2);

		if (Util.getShare(this, "user") == null
				|| Util.getShare(this, "user").equals("")) {// 登录前和登陆后状态
			loginout();
		} else {
			login();
		}

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_LOGININ);
		datareceiver = new DataReceiver();
		registerReceiver(datareceiver, filter);

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				user = username.getText().toString();
				pass = password.getText().toString();

				if (!Util.isexist(numberpattern, user)) {
					Util.showToast(LoginActivity.this, "必须输入正确手机号码");
					return;
				}
				if (!Util.isexist(passwordpattern, pass)) {
					Util.showToast(LoginActivity.this, "请输入3位以上密码,不允许空格");
					return;
				}

				Bundle bd = new Bundle();
				bd.putString("user", user);
				bd.putString("pass", pass);
				if (mBound == false)
					return;
				HomeActivity.service.login(LoginActivity.this,
						new Intent().putExtras(bd));
				mBound = false;
				dialog.show();
			}
		});

		regist.setOnClickListener(new OnClickListener() {// 注册

			@Override
			public void onClick(View v) {
				Util.startactivity(LoginActivity.this, RegistActivity.class);
			}
		});

		findpassword.setOnClickListener(new OnClickListener() {// 找回密码
					@Override
					public void onClick(View v) {
						Util.startactivity(LoginActivity.this,
								FindPasswordActivity.class);
					}
				});

		loginout.setOnClickListener(new OnClickListener() {// 登出
			@Override
			public void onClick(View v) {
				Util.saveShare(LoginActivity.this, "user", "");
				Util.saveShare(LoginActivity.this, "pass", "");
				loginout();
			}
		});

		bound.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mBound == false)
					return;
				dialog.show();
				mBound = false;
				HomeActivity.service.bindAccount(LoginActivity.this, null);

			}
		});

		// qq登录
		qq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				qqapi.authorize();
			}
		});

		// qq登录
		sina.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sinaapi.authorize();
			}
		});

	}

	// 接收器
	private class DataReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			int action = bd.getInt("action");
			dialog.hide();
			mBound = true;
			switch (action) {
			case 1:// 登录账号
				if (bd.getString("ret") == null) {
					Util.showToast(LoginActivity.this, "出错了");
				} else if (bd.getString("ret").equals("1")) {
					Util.showToast(LoginActivity.this, "登陆成功");
					Util.saveShare(LoginActivity.this, "user", user);
					Util.saveShare(LoginActivity.this, "pass", pass);
					login();
				} else if (bd.getString("ret").equals("100")) {
					Util.showToast(LoginActivity.this, "此用户不存在");
				} else if (bd.getString("ret").equals("101")) {
					Util.showToast(LoginActivity.this, "密码错误");
				}
				break;
			case 2:// 绑定账号
				if (bd.getString("ret") == null) {
					Util.showToast(LoginActivity.this, "出错了");
				} else if (bd.getString("ret").equals("1")) {
					Util.showToast(LoginActivity.this, "绑定成功");
				} else if (bd.getString("ret").equals("100")) {
					Util.showToast(LoginActivity.this, "mac账号不存在");
				} else if (bd.getString("ret").equals("101")) {
					Util.showToast(LoginActivity.this, "account不存在");
				} else if (bd.getString("ret").equals("102")) {
					Util.showToast(LoginActivity.this, "密码错误");
				}
				break;
			}
		}
	}

	@Override
	protected void onDestroy() {
		if (dialog != null)
			dialog.cancel();
		if (datareceiver != null)
			unregisterReceiver(datareceiver);
		qqapi.unReceiver();//注销qqapi的recevier
		super.onDestroy();
	}

	public void loginout() {// 登出
		login2.setVisibility(View.GONE);
		login1.setVisibility(View.VISIBLE);
		username.setText("");
		password.setText("");
		title.setText("登录页面");
	}

	public void login() {// 登录后
		login1.setVisibility(View.GONE);
		login2.setVisibility(View.VISIBLE);
		title.setText("你好," + Util.getShare(this, "user"));

	}

}
